package com.woniuxy.staffservice.web;

import com.woniuxy.staffservice.entity.Dept;
import com.woniuxy.staffservice.service.DeptService;
import commons.DataGridView;
import commons.LayuiJson;
import commons.ResultObj;
import commons.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    LayuiJson layuiJson;


    @PostMapping("loadDeptManagerLeftTreeJson")
    public DataGridView loadManagerLeftTreeJson() {
        //查询出所有的部门，存放进list中
        List<Dept> list = deptService.list();

        List<TreeNode> treeNodes = new ArrayList<>();
        //将部门放入treeNodes中，组装成json
        for (Dept dept : list) {
            treeNodes.add(new TreeNode(dept.getId(), dept.getPid(), dept.getName(), true));
        }
        return new DataGridView(treeNodes);
    }

    @PostMapping("addDept")
    public ResultObj addDept(Dept dept) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
            sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();// 获取当前时间
            dept.setCreateTime(sdf.format(date));
            deptService.add(dept);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @GetMapping("loadAllDept")
    public LayuiJson<Dept> select(String name, String address, String description
            , Integer page, Integer limit) {
        int index = (page - 1) * limit;
        List<Dept> depts = deptService.getByCondition(name, address, description, index, limit);
        layuiJson.setCount(1000);
        layuiJson.setData(depts);
        layuiJson.setCode(0);
        return layuiJson;

    }

    /**
     * 更新部门
     *
     * @param
     * @return
     */
    @PostMapping("updateDept")
    public ResultObj updateDept(Dept dept) {
        try {
            deptService.updateById(dept);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 检查当前部门是否有子部门
     *
     * @param
     * @return
     */
    @RequestMapping("checkDeptHasChildrenNode")
    public Map<String, Boolean> checkDeptHasChildrenNode(Integer id) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        List<Dept> list = deptService.getByPid(id);
        if (list.size() > 0) {
            map.put("value", true);
        } else {
            map.put("value", false);
        }
        return map;
    }

    /**
     * 删除部门
     */
    @RequestMapping("deleteDept")
    public ResultObj deleteDept(Integer id) {
        try {
            deptService.deleteById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
